package src.io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NIOSocketClientIO {
    public static void main(String[] args) throws InterruptedException {
        try {
            SocketChannel socketChannel=SocketChannel.open();
            socketChannel.configureBlocking(false);
            socketChannel.connect(new InetSocketAddress(8081));
            if (socketChannel.isConnectionPending()){
                socketChannel.finishConnect();
            }

            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);

            byteBuffer.put("jj is good".getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);

            byteBuffer.clear();
            int r= socketChannel.read(byteBuffer);
            Thread.sleep(11000L);
            if(r>0){
                System.out.println("收到服务端消息:"+new String(byteBuffer.array()));
            }else {
                System.out.println("还没收到服务端消息");

            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
